<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/7
 * Time: 14:25
 */

namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;
use doumiaoduo\buildform\BuildForm;
use yii\helpers\Json;
use zhtfrontend\components\bases\BaseController;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Tutor;
use yii\data\Pagination;
use yii\web\Response;
use yii\web\NotFoundHttpException;
use common\components\actions\DeleteAction;
use yii\web\BadRequestHttpException;
class TutorController extends BaseController{

    public function actionIndex()
    {
        if (\Yii::$app->request->isAjax) {
            if (\Yii::$app->request->post('param') != null) $param = Json::decode(\Yii::$app->request->post('param'));
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
            $ajax_object = new AjaxObject();
            $data = Tutor::find()->orderBy('id DESC');
            if (!empty($param['searchData'])) {
                $search_data = array_filter($param['searchData']);
                if (!empty($search_data['name'])) {
                    $data->andWhere(['like', 'name', $search_data['name']]);
                }

            }
            $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $data->offset($pages->offset)->limit($pages->limit)->all();
            $array=Fun::asArray($model, ['operate']);
            $ajax_object->data =$array;
            $ajax_object->pages = $pages;
            return $ajax_object;
        }

        return $this->render('index');
    }
    /*
     * 老师添加
     */
    public function actionCreate(){
        $this->layout=false;
        $model=new Tutor();
        $form=new BuildForm('@app/components/forms/tutorForm.php');
        $form->loadModel(['tutor'=>$model]);
        $form->loadDefaultValues();

        if($form->loadData(\Yii::$app->request->post())&&$form->validate()){
            $post = \Yii::$app->request->post();
            if($model->save(false)){
                return $this->redirectMessage('添加成功',Fun::url(['tutor/index']));
            }
        }
        return $this->render('create',['form'=>$form->render()]);
    }


    public function actionUpdate(){
        $this->layout=false;
        if(\Yii::$app->request->get('id')==null){
            throw new BadRequestHttpException(\Yii::t('zhtfrontend','信息不存在'));
        }
        $model=Tutor::findOne(\Yii::$app->request->get('id'));
        if( $model==null){
            throw new NotFoundHttpException(\Yii::t('zhtfrontend','信息不存在'));
        }
        $form=new BuildForm('@app/components/forms/tutorForm.php');
        $form->loadModel(['tutor'=>$model]);
        $form->loadDefaultValues();
        if( $form->loadData(\Yii::$app->request->post()) && $form->validate()){
            $post = \Yii::$app->request->post();
            if($model->save(false)){
                return $this->redirectMessage(\Yii::t('zhtfrontend','修改成功'),Fun::url(['tutor/index']));
            }
        }

        return $this->render('create',['form'=>$form->render()]);
    }

    public function actions(){
        return [
            'delete' => [
                'class' => DeleteAction::className(),
                'modelClass' => Tutor::className(),
                'message' => ['success'=>'删除成功！'],
                'redirectTo' => 'tutor/index',
            ],
        ];
    }


}
